import { defineConfig, loadEnv, UserConfig, ConfigEnv } from 'vite'
import path from 'path'
import vue from '@vitejs/plugin-vue'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
// https://vitejs.dev/config/

const resolve = (dir: string) => path.resolve(__dirname, dir)
const viteConfig = defineConfig(({ mode }: ConfigEnv): UserConfig => {
  const { VITE_BASE_PATH, VITE_BASE_API } = loadEnv(mode, process.cwd())
  return {
    plugins: [vue({
      script: {
        defineModel: true,
      }
    }), VueSetupExtend()],
    base: VITE_BASE_PATH, //打包路径
    publicDir: resolve('public'),
    root: process.cwd(), //根目录路径
    resolve: {
      // 路径别名
      alias: {
        '@': resolve('src')
      },
      // 导入时想要省略的扩展名列表 //默认忽略以下
      extensions: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
    },
    //启动服务配置
    server: {
      host: '0.0.0.0',
      //启动端口
      port: 8085,
      // 自动在浏览器打开
      open: false,
      // 设置 https 代理
      proxy: {
        '/api': {
          target: VITE_BASE_API,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      },
    },
  }
})
export default viteConfig  